﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using SensorDrone.Net;
using SensorDroneStudio.Common;
using SensorDroneStudio.Services;
using SimpleMvvmToolkit;

namespace SensorDroneStudio.ViewModels
{
    public class DroneSelectorViewlModel : ViewModelBase<DroneSelectorViewlModel>
    {
        private readonly ISensorDroneService _service;
        private string _scanText = "";
        private bool _scanning;
        private DroneSummaryViewModel _selected;
        private ObservableCollection<DroneSummaryViewModel> _viewModels;

        public DroneSelectorViewlModel(ISensorDroneService service)
        {
            _service = service;
            Drones = new ObservableCollection<DroneSummaryViewModel>();
            Drones.CollectionChanged += ViewModelsCollectionChanged;
            Scan = new DelegateCommand(ScanDrones);
            ScanText = "scan";
            PropertyChanged += OnPropertyChanged;
        }

        public ICommand Scan { get; private set; }

        public string ScanText
        {
            get { return _scanText; }
            set
            {
                _scanText = value;
                NotifyPropertyChanged(m => ScanText);
            }
        }

        public bool Scanning
        {
            get { return _scanning; }
            set
            {
                _scanning = value;
                NotifyPropertyChanged(m => Scanning);
                NotifyPropertyChanged(m => Visible);
            }
        }

        public Visibility Visible
        {
            get { return Scanning ? Visibility.Visible : Visibility.Hidden; }
        }

        public ObservableCollection<DroneSummaryViewModel> Drones
        {
            get { return _viewModels; }
            set
            {
                _viewModels = value;
                NotifyPropertyChanged(m => Drones);
            }
        }

        public DroneSummaryViewModel Selected
        {
            get { return _selected; }
            set
            {
                if (_selected != value)
                {
                    _selected = value;
                    if (_selected != null)
                    {
                        SendMessage(MessageTokens.DroneSelected,
                            new NotificationEventArgs<ISensorDrone>("", _selected.Model));
                    }
                }
            }
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Scanning")
            {
            }
        }

        private void ViewModelsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            NotifyPropertyChanged(m => m.Drones);
        }

        public void DroneChanged()
        {
        }

        private async void ScanDrones()
        {
            _service.Stop();
            SendMessage(MessageTokens.Scanning, new NotificationEventArgs(MessageTokens.Scanning));
            Drones.Clear();
            Scanning = true;
            IEnumerable<ISensorDrone> drones = await _service.ScanAsync();

            foreach (ISensorDrone drone in drones)
            {
                Drones.Add(new DroneSummaryViewModel(drone));
            }
            Scanning = false;
        }
    }
}